package comoabejas;



import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import com.sun.image.codec.jpeg.JPEGImageEncoder
import javax.imageio.ImageIO
import java.awt.Image
import java.awt.image.BufferedImage;

import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource

class CrearDealTest {

	DealService dealService
	AreaService areaService
	UserService userService
	ClientService clientService
	ImageService imageService
	
	@Before
	public void setUp() throws Exception {
	}

	public void testCrearDeal(){
		Area a = new Area()
		User u = new User()
		Deal d = new Deal()
		Client c = new Client()
		
		a.setName("Lanus")
		a.setDescription ""
		a.setType ""
		
		Area area = areaService.saveObj(a)
		
		
		u.setEmail "matiaskochman@yahoo.com.ar"
		u.setName "Matias"
		u.setPassword "matias1"
		u.setUsername "matiaskochman"
		u.setSurname "Kochman"
		
		userService.saveObj(u)
		
		c.setCompanyName "Kotex"
		c.setLocalidad "Lanus"
		c.setEmail1 "kotex@casdf.com"
		c.setAddress1 ""
		c.setContactName1 ""
		c.setCodigoPostal ""
		c.setContactPhone1 ""
		
		
		clientService.saveObj(c)
		
		Resource resource = new ClassPathResource("resources/tropa.jpg") 
		def file = resource.getFile() 
		assert file.exists()
		
		Imagen image = new Imagen()
		image.setMyFile(file.getBytes())
		
		imageService.saveObj(image)
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1)
		
		d.setAddress "sarlanga"
		d.setTitle("test integracion")
		d.setConditions ""
		d.setDescription ""
		d.setDetails ""
		
		d.setArea(area)
		d.setClient(c)
		d.setStartDate(cal.getTime())
		
		cal.add(Calendar.DAY_OF_MONTH, 1)
		
		d.setEndDate(cal.getTime())
		
		cal.add(Calendar.DAY_OF_MONTH, 1)
		
		d.setVoucherStartDate(cal.getTime())
		cal.add(Calendar.DAY_OF_MONTH, 1)
		
		d.setVoucherEndDate(cal.getTime())
		
		d.setDiscountPrice(20f)
		d.setNormalPrice(100f)
		d.setRequiredNumberOfUsers(5)
		d.setCurrentNumberOfUsers(0)
		d.setState(DealState.ACTIVE)
		d.setImage(image)
		d.setDateOfCreation(new Date())
		
		
		Deal deal = dealService.saveObj(d)
		
		def dealList = Deal.findAllByState("ACTIVE", [sort:"dateOfCreation",order:"desc"]);
		
		if(dealList ==null){
			throw new RuntimeException("no encontro un deal en estado active");
		}
		
		Deal deal1 = dealList.get(0)

			
	}
	
	@After
	public void tearDown() throws Exception {
	}

}
